package earogya.pharmacy.patient.ws;

import java.io.IOException;

import org.hibernate.HibernateException;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v251.message.EQQ_Q04;
import ca.uhn.hl7v2.model.v251.message.RDE_O11;
import ca.uhn.hl7v2.model.v251.message.RDS_O13;
import ca.uhn.hl7v2.model.v251.segment.MSH;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;
import earogya.pharmacy.patient.messages.EQQ_Q04_Handler;
import earogya.pharmacy.patient.messages.RDE_O11_Handler;
import earogya.pharmacy.patient.messages.RDS_O13_Handler;

/* HL7 message handler
 * 
 * reads the messages coming from the ESB and directs them to the correct message handler
 * for further execution and extraction of data
 * 
 * */
public class HL7MessageHandler {

	public String parser(String message) {
		PipeParser p = new PipeParser();
		String ack = null;
		Message hapiMsg;

		try {
			hapiMsg = p.parse(message);
			MSH mshSegment = (MSH) hapiMsg.get("MSH");

			String msgType = mshSegment.getMessageType().getMessageCode()
					.getValue();

			String msgTrigger = mshSegment.getMessageType().getTriggerEvent()
					.getValue();
			ack = identifyMessage(msgType, msgTrigger, hapiMsg);
			System.out.println(msgType + " " + msgTrigger);
		} catch (EncodingNotSupportedException e) {
			e.printStackTrace();
		} catch (HL7Exception e) {
			e.printStackTrace();
		} catch (HibernateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ack;
	}

	private String identifyMessage(String messageCode, String triggerEvent,
			Message hapiMsg) throws HibernateException, IOException,
			HL7Exception {
		String ack = null;
		if (messageCode.equals("RDE")) {
			ack = this.identifyTriggerEventForRDE(triggerEvent, hapiMsg);
		} else if (messageCode.equals("RDS")) {
			ack = this.identifyTriggerEventForRDS(triggerEvent, hapiMsg);
		} else if (messageCode.equals("EQQ")) {
			ack = this.identifyTriggerEventForEQQ(triggerEvent, hapiMsg);
		}
		else {
			System.out.println("Unexpected tigger event");
		}
		return ack;
	}

	private String identifyTriggerEventForRDS(String triggerEvent,
			Message hapiMsg) throws HibernateException, IOException,
			HL7Exception {
		String ack = null;
		if (triggerEvent.equals("O13")) {
			RDS_O13 adtMsg = (RDS_O13) hapiMsg;
			RDS_O13_Handler order = new RDS_O13_Handler(adtMsg);
			ack = order.manageMessage();
		} else {
		}
		return ack;
	}
	
	private String identifyTriggerEventForEQQ(String triggerEvent,
			Message hapiMsg) throws HibernateException, IOException,
			HL7Exception {
			String ack = null;
			
				EQQ_Q04 adtMsg = (EQQ_Q04) hapiMsg;
				EQQ_Q04_Handler qry = new EQQ_Q04_Handler(adtMsg);
				ack = qry.manageMessage();
			
		return ack;
	}

	private String identifyTriggerEventForRDE(String triggerEvent,
			Message hapiMsg) throws HibernateException, IOException,
			HL7Exception {
		String ack = null;
		if (triggerEvent.equals("O11")) {
			RDE_O11 adtMsg = (RDE_O11) hapiMsg;
			RDE_O11_Handler order = new RDE_O11_Handler(adtMsg);
			ack = order.manageMessage();
		} else {
		}
		return ack;
	}
}